package com.settlement.system.service.impl;

import com.settlement.system.common.annotation.Cache;
import com.settlement.system.mapper.BaseMapper;
import com.settlement.system.mapper.TblRrBillTypeMapper;
import com.settlement.system.common.exception.ServiceException;
import com.settlement.system.model.entity.common.TblRrBillType;
import com.settlement.system.common.util.common.SystemUserUtil;
import com.settlement.system.service.ITblRrBillTypeService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import jakarta.annotation.Resource;
import java.util.List;

/**
 * RR-账单类型 服务实现类
 * @author xiaochuan
 * @since 2022-12-12
 */

@Slf4j
@Service
public class TblRrBillTypeServiceImpl extends BaseServiceImpl implements ITblRrBillTypeService {

    @Resource
    private TblRrBillTypeMapper tblRrBillTypeMapper;

    @Override
    public BaseMapper init() {
        return tblRrBillTypeMapper;
    }

    @Override
    public List<TblRrBillType> selectByParams(TblRrBillType tblRrBillType) throws ServiceException {
        try {
            return tblRrBillTypeMapper.selectByParams(tblRrBillType);
        } catch (Exception ex){
            log.error("selectByParams by tblRrBillType happen exception",ex);
            throw new ServiceException("selectByParams by tblRrBillType happen exception",ex);
        }
    }

    @Override
    public Integer updateCode() throws ServiceException {
        try {
            return tblRrBillTypeMapper.updateCode();
        } catch (Exception ex){
            log.error("updateCode exception",ex);
            throw new ServiceException("updateCode exception",ex);
        }
    }

    @Override
    public <ModelType> Integer insert(ModelType modelType) throws ServiceException {
        Integer insert = super.insert(modelType);
        return insert;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer batchDisable(List<TblRrBillType> list) throws ServiceException {
        try {
            Integer disableNumber = 0;
            for(TblRrBillType info:list){
                SystemUserUtil.setDefualt(info);
                disableNumber += tblRrBillTypeMapper.disable(info);
            }
            return disableNumber;
        } catch (Exception ex){
            log.error("batchDisable by tblRrBillType happen exception",ex);
            throw new ServiceException("batchDisable by tblRrBillType happen exception",ex);
        }

    }

    @Override
    public String getBillCategoryByNo(String no) throws ServiceException {
        return getBillCategoryByCode(no);
    }

    private String getBillCategoryByCode(String code) throws ServiceException {
        try {
            return tblRrBillTypeMapper.getBillCategoryByCode(code);
        } catch (Exception ex) {
            log.error("getBillCategoryByCode exception", ex);
            throw new ServiceException("getBillCategoryByCode exception", ex);
        }
    }

    @Cache
    @Override
    public String getNoByName(String billType) throws ServiceException {
        try {
            return tblRrBillTypeMapper.getCodeByBillType(billType);
        } catch (Exception ex) {
            log.error("getNoByName exception", ex);
            throw new ServiceException("getNoByName exception", ex);
        }
    }

    @Cache
    public String getNameByNo(String code) {
        try {
            return tblRrBillTypeMapper.getBillTypeByCode(code);
        } catch (Exception ex) {
            log.error("getNameByNo exception", ex);
            throw new RuntimeException("getNameByNo exception", ex);
        }
    }

    @Cache
    @Override
    public TblRrBillType getByName(String name) throws ServiceException {
        try {
            return tblRrBillTypeMapper.getByBillType(name);
        } catch (Exception ex) {
            log.error("getByName exception", ex);
            throw new ServiceException("getByName exception", ex);
        }
    }


}

